/**
 * 2018/10/31 星期三 10:48:43 Lynn Li created.
 */

package com.diver.flinkdemo.service.impl;


import com.diver.flinkdemo.dto.xxt.MonthExerciseInfo;
import com.diver.flinkdemo.entity.xxt.MonthExercise;
import com.diver.flinkdemo.mapper.mysqlMapper.MonthExerciseMapper;
import com.diver.flinkdemo.service.IMonthExerciseService;
import com.diver.flinkdemo.utils.DateUtil;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

/**
 * 月运动量 Service 实现
 * Created by Lynn Li on 2018/10/31.
 */
//@Service
@Slf4j
public class MonthExerciseServiceImpl implements IMonthExerciseService {


//    @Autowired
	private MonthExerciseMapper mapper;
	private SqlSession session ;


	@Override
	public void doUpdateMonthExercise(MonthExercise monthExercise) {
		try {
			session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
			mapper = session.getMapper(MonthExerciseMapper.class);
			mapper.doUpdateMonthExercise(monthExercise, DateUtil.getStartOfDay(LocalDate.now()));
		} finally {
			endOfSession(session);
		}
	}

	@Override
	public List<MonthExerciseInfo> getMonthlySportByStudentCardId(String studentCardId) {
		try {
			session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
			mapper = session.getMapper(MonthExerciseMapper.class);
			return mapper.getMonthlySportByStudentCardId(studentCardId);
		} finally {
			endOfSession(session);
		}
	}

	@Override
	public void doUpdateStepById(MonthExercise monthExercise) {
		try {
			session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
			mapper = session.getMapper(MonthExerciseMapper.class);
			mapper.doUpdateStep(monthExercise);
		} finally {
			endOfSession(session);
		}
	}

	public void endOfSession(SqlSession session) {
		//关闭sqlSession
		if (session != null) {
			log.info("session commit -> session close");
//			session.commit();
			session.close();
		}
	}
}
